package org.lanqiao.selenium;

import com.demotoweb.utils.EmailUtil;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.JobWithDetails;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.net.URI;

import static com.demotoweb.utils.EmailUtil.SMTP_QQ;


public class RunSeleniumLanQiao {
    public WebDriver driver;
    /**
     * 显示等待
     */
    protected WebDriverWait wait;

    // 该邮箱修改为你需要发送的邮箱地址
    private static final String TO_EMAIL = "XX@qq.com";

    @BeforeTest
    public void before() {
        // 配置对应的QQ-SMIP服务
        EmailUtil.config(SMTP_QQ(true), "yy@qq.com", "password");
    }

    @Test
    public void runSeleniumLanQiao() throws InterruptedException {
        /**
         * 前置操作加载火狐驱动
         */
    	System.setProperty("webdriver.gecko.driver", "D:\\opt\\firefox\\geckodriver.exe");
        driver = new FirefoxDriver();
        System.out.println(driver);
        driver.get("https://www.lanqiao.cn/");
        /* 窗口最大化 */
        driver.manage().window().maximize();
        //死等
        Thread.sleep(1000);
        System.out.println(driver.getTitle());
        //WebElement元素对象
        WebElement webElement = driver.findElement(By.cssSelector(".sign-btn.signin"));
        webElement.click();
        driver.findElement(By.cssSelector(".cursor-pointer > span")).click();
        driver.findElement(By.cssSelector(".ant-form-item-children > .ant-input")).click();
        driver.findElement(By.cssSelector(".ant-form-item-children > .ant-input")).sendKeys("123456");
        driver.findElement(By.cssSelector(".ant-input-affix-wrapper > .ant-input")).click();
        driver.findElement(By.cssSelector(".ant-input-affix-wrapper > .ant-input")).sendKeys("123456");
        driver.findElement(By.cssSelector(".mb-32px > .\\!text-18px")).click();
        driver.quit();
    }

    /**
     * 测试完成后发送测试报告邮箱
     */
    @AfterTest
    public void  sendMail(){
        //创建jenkinsServer实例
        JenkinsServer jenkinsServer = null;
        try {
            jenkinsServer = new JenkinsServer(new URI("http://localhost:8888"), "tester", "tester");
            JobWithDetails job = jenkinsServer.getJob("pipline");
            //Jenkins 的构建编号
            int num = job.getNextBuildNumber()-1;
            //HTML 模板自定义设置-注意这里配置的是内网地址，如果需要外网访问需要配置外网地址
//            String template = "<a href=\"http://localhost:8888/job/pipline/"+num+"/allure/\" target=\"_blank\">迭代版本-V_1.0.1测试报告点击查看</a>";
            //邮件模板可定义
            String template = "<p>测试人员：tester</p>\n" +
                    "<p>测试进度：10%</p>\n" +
                    "<p>缺陷量：5</p>\n" +
                    "<p>今日新增bug：3 | 遗留bug：5</p>\n" +
                    "<p>需求变动：无</p>\n" +
                    "<p>是否存在风险：无</p>\n" +
                    "<a href=\"http://localhost:8888/job/pipline/"+num+"/allure/\" target=\"_blank\">迭代版本-V_1.0.1测试报告点击查看</a>";
            EmailUtil.subject("QA-测试报告-Allure报告查收")
                    .from("QA人员")
                    .to(TO_EMAIL)
                    .html(template)
                    .send();
            Assert.assertTrue(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
